package com.fescoservice.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.fescoservice.model.WorkerRequest;

/**
 * A data access object (DAO) providing persistence and search support for
 * WorkerRequest entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.fescoservice.dao.WorkerRequest
 * @author MyEclipse Persistence Tools
 */

public class WorkerRequestDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(WorkerRequestDAO.class);
	// property constants
	public static final String USER_ID = "userId";
	public static final String CUSTOMER_NAME = "customerName";
	public static final String SEXUAL = "sexual";
	public static final String CUSTOMER_CORP = "customerCorp";
	public static final String LANGUAGE = "language";
	public static final String NATIONALITY = "nationality";
	public static final String PHONE = "phone";
	public static final String FAX = "fax";
	public static final String CUSTOMER_INTRO = "customerIntro";
	public static final String REQUEST_DESCRIPTION = "requestDescription";
	public static final String MEMO = "memo";
	public static final String STATUS = "status";
	public static final String AGE = "age";

	protected void initDao() {
		// do nothing
	}

	public void save(WorkerRequest transientInstance) {
		log.debug("saving WorkerRequest instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(WorkerRequest persistentInstance) {
		log.debug("deleting WorkerRequest instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public WorkerRequest findById(java.lang.Long id) {
		log.debug("getting WorkerRequest instance with id: " + id);
		try {
			WorkerRequest instance = (WorkerRequest) getHibernateTemplate()
					.get("com.fescoservice.model.WorkerRequest", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(WorkerRequest instance) {
		log.debug("finding WorkerRequest instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding WorkerRequest instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from WorkerRequest as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	public List findByCustomerName(Object customerName) {
		return findByProperty(CUSTOMER_NAME, customerName);
	}

	public List findBySexual(Object sexual) {
		return findByProperty(SEXUAL, sexual);
	}

	public List findByCustomerCorp(Object customerCorp) {
		return findByProperty(CUSTOMER_CORP, customerCorp);
	}

	public List findByLanguage(Object language) {
		return findByProperty(LANGUAGE, language);
	}

	public List findByNationality(Object nationality) {
		return findByProperty(NATIONALITY, nationality);
	}

	public List findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List findByFax(Object fax) {
		return findByProperty(FAX, fax);
	}

	public List findByCustomerIntro(Object customerIntro) {
		return findByProperty(CUSTOMER_INTRO, customerIntro);
	}

	public List findByRequestDescription(Object requestDescription) {
		return findByProperty(REQUEST_DESCRIPTION, requestDescription);
	}

	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findByAge(Object age) {
		return findByProperty(AGE, age);
	}

	public List findAll() {
		log.debug("finding all WorkerRequest instances");
		try {
			String queryString = "from WorkerRequest";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public WorkerRequest merge(WorkerRequest detachedInstance) {
		log.debug("merging WorkerRequest instance");
		try {
			WorkerRequest result = (WorkerRequest) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(WorkerRequest instance) {
		log.debug("attaching dirty WorkerRequest instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(WorkerRequest instance) {
		log.debug("attaching clean WorkerRequest instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static WorkerRequestDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (WorkerRequestDAO) ctx.getBean("WorkerRequestDAO");
	}
}